package com.company.sendemo.basics.ayJavaEight.stream;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class stream {
    /**
     * 流是一个元素队列，支持聚合操作
     * 流让你以一种声明的方式处理数据
     * 流在管道中传输 并且可以在管道的节点上进行处理，比如筛选，排序，聚合等  filter sorted map collect
     */
//    List<Integer> transactionsIds =
//            widgets.stream()
//                    .filter(b -> b.getColor() == RED)
//                    .sorted((x,y) -> x.getWeight() - y.getWeight())
//                    .mapToInt(Widget::getWeight)
//                    .sum();

    //集合接口有两个方法来生成流
    public static void main(String[] args) throws IOException{
        //todo foreach  limit
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList()).forEach(System.out::println);

        Random random = new Random();
        random.ints().limit(3).forEach(System.out::println);

        //todo map
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
         // 获取对应的平方数
        List<Integer> collect = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());

        /**
         * .distinct() 不同的
//         */
        for (Integer squeresList : collect){
            System.out.println(squeresList);
        }

        //todo filter
        List<String> stringss = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        // 获取空字符串的数量
        long count = stringss.stream().filter(string -> string.isEmpty()).count();
        System.out.println("获取空字符串的数量"+count);

        //todo sorted 随机生成树 排序
        Random random1 = new Random();
        random.ints().limit(10).sorted().forEach(System.out::println);

        //todo 并行(parallel) 程序
        List<String> stringh = Arrays.asList("abc", "","","bc", "bc", "efg", "abcd","", "jkl");
        // 获取空字符串的数量
        long count1 = stringh.parallelStream().filter(string -> string.isEmpty()).count();
        System.out.println("为空字符串数量"+count1);

        //TODO Collectors 流转换集合和聚合元素
        List<String> stringhh = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List<String> filtered = stringhh.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println("筛选列表: " + filtered);
        String mergedString = stringhh.stream().filter(s -> !s.isEmpty()).collect(Collectors.joining(""));
        System.out.println("合并字符串: " + mergedString);

        //todo 统计
        List<Integer> numberss = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        IntSummaryStatistics stats = numberss.stream().mapToInt((x) -> x).summaryStatistics();
        System.out.println("列表中最大的数 : " + stats.getMax());
        System.out.println("列表中最小的数 : " + stats.getMin());
        System.out.println("所有数之和 : " + stats.getSum());
        System.out.println("平均数 : " + stats.getAverage());

        /**
         * 读取控制台输入
         * Java 的控制台输入由 System.in 完成
         * 使用 BufferedReader 在控制台读取字符
         */
        read();
        /**
         * 从控制台读取字符串
         * 从标准输入读取一个字符串需要使用 BufferedReader 的 readLine() 方法。
         */
        read2();
        /**
         * 控制台输出
         */
        write();

    }
    public static void read()throws IOException{
        char c;
        //使用 System.in 创建 BufferedReader
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入字符, 按下 'q' 键退出。");
        //读取字符
        do {
            c = (char) bufferedReader.read();
            System.out.println(c);
        }while (c!= 'q');
    }
    public static void read2()throws IOException{
        //使用 System.in 创建 BufferedReader
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String str;
        System.out.println("Enter lines of text.");
        System.out.println("Enter 'end' to quit.");
        do {
           str = bufferedReader.readLine();
            System.out.println(str);
        }while (!str.equals("end"));
    }
    public static void write(){
        int b = 'A';
        System.out.write(b);
        System.out.println("B");
    }
}
